/**
 * @Project Name: ${projectName}
 * @Package Name: ${projectPackageName}.infrastructure.serviceimpl.${domainName}
 * @Since JDK 1.8
 */
package ${projectPackageName}.infrastructure.serviceimpl.${domainName};

import cn.com.scgeling.framework.pagination.domain.ConditionAndPagination;
import cn.com.scgeling.framework.pagination.domain.Pagination;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${projectPackageName}.infrastructure.convert.${domainName}.${className}Converter;
import ${projectPackageName}.domain.dto.${domainName}.${className}DTO;
import ${projectPackageName}.infrastructure.model.${domainName}.${className}PO;
import ${projectPackageName}.infrastructure.mapper.${domainName}.${className}Mapper;
import ${projectPackageName}.infrastructure.service.${domainName}.${className}Service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: ${className}ServiceImpl
 * @Author 胡松 hoofungson@163.com
 * @Date ${datetime}
 * @Version V1.0
 */
@Slf4j
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}PO> implements ${className}Service {

    /**
    * 保存或更新
    *
    * @param ${lowerCaseClassName}DTO
    **/
    @Override
    public void saveOrUpdate(${className}DTO ${lowerCaseClassName}DTO) {
        ${className}PO ${lowerCaseClassName}PO = new ${className}PO();
        BeanUtils.copyProperties(${lowerCaseClassName}DTO, ${lowerCaseClassName}PO);
        if (null == ${lowerCaseClassName}PO.getCreateTime()) {
            ${lowerCaseClassName}PO.setCreateTime(LocalDateTime.now());
        }
        ${lowerCaseClassName}PO.setUpdateTime(LocalDateTime.now());
        super.saveOrUpdate(${lowerCaseClassName}PO);
    }

    /**
    * 批量保存或更新
    *
    * @param ${lowerCaseClassName}DTOList
    **/
    @Override
    public void batchSaveOrUpdate(List<${className}DTO> ${lowerCaseClassName}DTOList) {
        if (CollectionUtils.isEmpty(${lowerCaseClassName}DTOList)) {
            return;
        }
        List<${className}PO> ${lowerCaseClassName}POList = new LinkedList<>();
            ${lowerCaseClassName}DTOList.forEach(${lowerCaseClassName}DTO -> {
            ${className}PO ${lowerCaseClassName}PO = new ${className}PO();
            BeanUtils.copyProperties(${lowerCaseClassName}DTO, ${lowerCaseClassName}PO);
            if (null == ${lowerCaseClassName}PO.getCreateTime()) {
                ${lowerCaseClassName}PO.setCreateTime(LocalDateTime.now());
            }
            ${lowerCaseClassName}PO.setUpdateTime(LocalDateTime.now());
            ${lowerCaseClassName}POList.add(${lowerCaseClassName}PO);
        });
        super.saveOrUpdateBatch(${lowerCaseClassName}POList);
    }

    /**
    * 根据ID获取
    *
    * @param id
    * @return
    **/
    @Override
    public ${className}DTO getById(Long id) {
        ${className}PO ${lowerCaseClassName}PO = super.getById(id);
        if (null == ${lowerCaseClassName}PO) {
            return null;
        }
        ${className}DTO ${lowerCaseClassName}DTO = new ${className}DTO();
        BeanUtils.copyProperties(${lowerCaseClassName}PO, ${lowerCaseClassName}DTO);
        return ${lowerCaseClassName}DTO;
    }

    /**
    * 根据条件获取
    *
    * @param condition
    * @return
    **/
    @Override
    public ${className}DTO getByCondition(${className}DTO condition) {
        Assert.isTrue(null != condition, "条件对象不能为空！！");
        log.info("入参: {}", JSONUtil.toJsonStr(condition));
        ${className}PO ${lowerCaseClassName}PO = super.getOne(new LambdaQueryWrapper<${className}PO>()
            .eq(null != condition.getId(), ${className}PO::getId, condition.getId())
        );
        if (null == ${lowerCaseClassName}PO) {
            return null;
        }
        ${className}DTO ${lowerCaseClassName}DTO = new ${className}DTO();
        BeanUtils.copyProperties(${lowerCaseClassName}PO, ${lowerCaseClassName}DTO);
        return ${lowerCaseClassName}DTO;
    }

    /**
    * 获取所有列表
    *
    * @return
    **/
    @Override
    public List<${className}DTO> listAll() {
         List<${className}PO> ${lowerCaseClassName}POList = super.list(new LambdaQueryWrapper<${className}PO>()
            .orderByAsc(${className}PO::getSort)
            .orderByDesc(${className}PO::getCreateDate)
        );
        if (CollectionUtils.isEmpty(${lowerCaseClassName}POList)) {
            return null;
        }
        List<${className}DTO> ${lowerCaseClassName}DTOList = new ArrayList<>();
        ${className}Converter.convert(${lowerCaseClassName}POList, ${lowerCaseClassName}DTOList);
        return ${lowerCaseClassName}DTOList;
    }

    /**
    * 根据条件获取列表
    *
    * @param condition
    * @return
    **/
    @Override
    public List<${className}DTO> findByCondition(${className}DTO condition) {
        Assert.isTrue(null != condition, "条件分页对象不能为空！！");
        List<${className}PO> ${lowerCaseClassName}POList = super.list(new LambdaQueryWrapper<${className}PO>()
            .eq(null != condition.getId(), ${className}PO::getId, condition.getId())
            .orderByAsc(${className}PO::getSort)
            .orderByDesc(${className}PO::getCreateTime)
        );
        if (CollectionUtils.isEmpty(${lowerCaseClassName}POList)) {
            return null;
        }
        List<${className}DTO> ${lowerCaseClassName}DTOList = new ArrayList<>();
        ${className}Converter.convert(${lowerCaseClassName}POList, ${lowerCaseClassName}DTOList);
        return ${lowerCaseClassName}DTOList;
    }

    /**
    * 获取分页
    *
    * @param conditionAndPagination
    * @return
    **/
    @Override
    public Pagination<${className}DTO> page(ConditionAndPagination<${className}DTO> conditionAndPagination) {
        Assert.isTrue(null != conditionAndPagination, "条件分页对象不能为空！！");
        Page<${className}PO> iPage = new Page<>(
            null != conditionAndPagination.getPage() ? conditionAndPagination.getPage() : 1L
            , null != conditionAndPagination.getLimit() ? conditionAndPagination.getLimit() : 10L);
        List<OrderItem> orderItemList = new ArrayList<>();
        orderItemList.add(OrderItem.asc("sort"));
        orderItemList.add(OrderItem.desc("create_time"));
        iPage.setOrders(orderItemList);

        ${className}DTO condition = conditionAndPagination.getCondition();
        IPage<${className}PO> page;
        if (null == condition) {
            page = super.page(iPage, new LambdaQueryWrapper<>());
        } else {
            page = super.page(iPage, new LambdaQueryWrapper<${className}PO>()
                .eq(null != condition.getId(), ${className}PO::getId, condition.getId())
            );
        }
        List<${className}PO> ${lowerCaseClassName}POList = page.getRecords();
        List<${className}DTO> ${lowerCaseClassName}DTOList = new ArrayList<>();
        ${className}Converter.convert(${lowerCaseClassName}POList, ${lowerCaseClassName}DTOList);
        return new Pagination<>(page.getSize(), page.getCurrent(), page.getPages(), page.getTotal(), ${lowerCaseClassName}DTOList);
    }

    /**
    * 根据ID删除
    *
    * @param id
    **/
    @Override
    public void deleteById(Long id) {
        super.removeById(id);
    }

    /**
    * 条件删除
    *
    * @param condition
    **/
    @Override
    public void deleteByCondition(${className}DTO condition) {
        if (null == condition) {
            return;
        }
        super.remove(new LambdaQueryWrapper<${className}PO>()
            .eq(null != condition.getId(), ${className}PO::getId, condition.getId())
        );
    }

}